﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataTool;
using workflowMVC.Models;
using DataTables;

namespace workflowMVC.Controllers
{
    public class NotificationController : Controller
    {
        public string getNotificationNumber()
        {
            wfdbEntities db = new wfdbEntities();
            int number = db.NOTIFICATIONs.Where(x => x.Username == User.Identity.Name && x.IsNew == true).Count();
            if (number != 0)
                return "<span class='notification'>" + number + @"</span>";
            else
                return "<span>" + number + @"</span>";
        }

        public ActionResult List()
        {
            ViewBag.datasource = Url.Action("ListData");
            return View();
        }

        public JsonResult ListData()
        {
            wfdbEntities db = new wfdbEntities();
            var data = db.NOTIFICATIONs.Where(x => x.Username == User.Identity.Name).AsEnumerable().Select(x => new NotificationListModel()
            {
                date = (DateTime)x.date,
                Content = x.Cont
            });
            foreach (var item in db.NOTIFICATIONs.Where(x => x.Username == User.Identity.Name).Where(x => x.IsNew == true))
            {
                item.IsNew = false;
            }
            db.SaveChanges();
            var cs = new DataTableParser<NotificationListModel>(Request, data.AsQueryable()).Parse();
            return Json(cs);
        }


    }
}
